/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.control.commons;

import org.apache.commons.beanutils.BeanUtils;
import org.mags.remas.model.commons.DataSourceInformation;
import org.mags.remas.model.entity.CommentType;
import org.mags.remas.model.entity.Entity;
import org.mags.remas.model.entity.EntityComment;
import org.mags.remas.model.entity.EntityReference;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.model.entity.EntityType;
import org.mags.remas.model.entity.KeyValue;
import org.mags.remas.model.entity.ListValue;
import org.mags.remas.model.entity.ReferenceStatus;

/**
 * 
 * @author Mario Gaitan
 *
 */
public final class Cloner {
	private Cloner(){};
	 
	public static Object clone(Object object) {
		if(object instanceof EntityReference)
			return clone((EntityReference)object);
		else if(object instanceof EntityComment)
			return clone((EntityComment)object);
		else
			return cloneObject(object);
	}
	private static Object cloneObject(Object object) {
		try {
			return BeanUtils.cloneBean(object);
		} catch(Exception e) {
			if(e instanceof RuntimeException)
				throw (RuntimeException)e;
			else
				throw new RuntimeException(e);
		}		
	}
	public static EntityReference clone(EntityReference object) {
		EntityReference cloneObject = (EntityReference)cloneObject(object);
		ReferenceStatus statuscloneObject = (ReferenceStatus)cloneObject(cloneObject.getReferenceStatus());
		cloneObject.setReferenceStatus(statuscloneObject);
		return cloneObject;		
	}
	public static EntityComment clone(EntityComment object) {
		EntityComment cloneObject = (EntityComment)cloneObject(object);
		CommentType typecloneObject = (CommentType)cloneObject(cloneObject.getCommentType());
		cloneObject.setCommentType(typecloneObject);
		return cloneObject;
	}
	public static DataSourceInformation clone(DataSourceInformation object) {
		return (DataSourceInformation)cloneObject(object);
	}
	public static ListValue clone(ListValue object) {
		return (ListValue)cloneObject(object);
	}
	public static Entity clone(Entity object) {
		return (Entity)cloneObject(object);
	}
	public static EntityRelationship clone(EntityRelationship object) {
		return (EntityRelationship)cloneObject(object);
	}
	public static EntityType clone(EntityType object) {
		return (EntityType)cloneObject(object);
	}
	public static KeyValue clone(KeyValue object) {
		return (KeyValue)cloneObject(object);
	}		
}
